﻿namespace A14_Ex01_2
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class MeuyanBuilder
    {
        private const int k_InitNumOfStars = 1;
        private const int k_TwoStars = 2;
        private const char k_SpaceChar = ' ';
        private static char m_StarCharToPrint = '*';
        private static StringBuilder m_StringBuilder;

        public static string CreateNewMeuyanStr(int i_NewMeuyanSize)
        {
            int numOfStars = k_InitNumOfStars;
            int numOfSpaces = calcNumOfSpaces(i_NewMeuyanSize);
            int triangleSize = calcUpperAndLowerTriangleSize(i_NewMeuyanSize);
            int maxMeuyamStrCapacity = i_NewMeuyanSize * i_NewMeuyanSize;
            m_StringBuilder = new StringBuilder(maxMeuyamStrCapacity);

            checkIfNewMeuyanSizeIsEven(ref i_NewMeuyanSize);

            for (int currentUpperTriangleRow = 1; currentUpperTriangleRow <= triangleSize; currentUpperTriangleRow++)
            {
                appendCharsToMeuyanStr(numOfSpaces, k_SpaceChar);
                appendCharsToMeuyanStr(numOfStars, m_StarCharToPrint);
                appendCharsToMeuyanStr(numOfSpaces, k_SpaceChar);
                m_StringBuilder.Append(System.Environment.NewLine);

                numOfStars += k_TwoStars;
                numOfSpaces--;
            }

            initTriangleVariables(i_NewMeuyanSize, ref numOfSpaces, ref numOfStars);

            for (int currentLowerTriangleRow = triangleSize; currentLowerTriangleRow >= 1; currentLowerTriangleRow--)
            {
                appendCharsToMeuyanStr(numOfSpaces, k_SpaceChar);
                appendCharsToMeuyanStr(numOfStars, m_StarCharToPrint);
                appendCharsToMeuyanStr(numOfSpaces, k_SpaceChar);
                m_StringBuilder.Append(System.Environment.NewLine);

                numOfStars -= k_TwoStars;
                numOfSpaces++;
            }

            return m_StringBuilder.ToString();
        }

        private static void initTriangleVariables(int i_NewMeuyanSize, ref int numOfSpaces, ref int numOfStars)
        {
            numOfSpaces = 1;
            numOfStars = i_NewMeuyanSize - 2;
        }

        private static void appendCharsToMeuyanStr(int i_NumOfChars, char i_CharToAppend)
        {
            for (int currentNumOfChar = 0; currentNumOfChar < i_NumOfChars; currentNumOfChar++)
            {
                m_StringBuilder.Append(i_CharToAppend);
            }
        }

        private static int calcNumOfSpaces(int i_NewMeuyanSize)
        {
            int result;

            return Math.DivRem(i_NewMeuyanSize, 2, out result);
        }

        private static int calcUpperAndLowerTriangleSize(int i_NewMeuyanSize)
        {
            int result;

            return Math.DivRem(i_NewMeuyanSize, 2, out result) + 1;
        }

        private static void checkIfNewMeuyanSizeIsEven(ref int io_NewMeuyanSize)
        {
            int result;

            Math.DivRem(io_NewMeuyanSize, 2, out result);

            if (Math.Equals(result, 0))
            {
                io_NewMeuyanSize++;
            }
        }
    }
}
